#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/wait.h>

int main()
{
    pid_t ret = fork();
    if(ret > 0)
    {
      while(true)
      {
        int cnt = 6;
        while(cnt--)
        {
            printf("I am father, pid:%d,ppid:%d\n", getpid(),getppid());
            sleep(1);
        }
        int status = 0;
    pid_t pid = waitpid(ret, &status, WNOHANG);
        if(pid == 0)
        {
             printf("no child to wait\n");
             continue;
        }
        else if(pid < 0) printf("error wait\n");
        else
        {
            if(WIFEXITED(status))
            {
                printf("child pid:%d,ret code:%d\n",pid, WEXITSTATUS(status));
            }
            else if(WIFSIGNALED(status))
            {
                printf("child pid:%d,signal code:%d\n",pid,WTERMSIG(status));
            }
            else
            {
                printf("child pid:%d,ret code:%d,signal code:%d\n",pid,(status >> 8) & 0xFF, status & 0x7F);
            }
            return 0;
        }
      }
    }
    else
    {
         while(true)
         {
             printf("I am child, pid:%d,ppid:%d\n", getpid(), getppid());
             sleep(1);
         }
    }    
    return 0;
}
